package org.jeecg.modules.achexampaperprojectitem.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.achexampaperprojectitem.entity.AchExamPaperProjectItem;
import org.jeecg.modules.achexampaperprojectitem.service.IAchExamPaperProjectItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.achexampaperprojectitem.vo.AchExamPaperProjectItemVo;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: ach_exam_paper_project_item
 * @author: baochangrong
 * @Date:   2022-03-16
 * @Version: V1.0
 */
@Api(tags="ach_exam_paper_project_item")
@RestController
@RequestMapping("/achexampaperprojectitem/achExamPaperProjectItem")
@Slf4j
public class AchExamPaperProjectItemController extends JeecgController<AchExamPaperProjectItem, IAchExamPaperProjectItemService> {
	@Autowired
	private IAchExamPaperProjectItemService achExamPaperProjectItemService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	 @Autowired
	 private ISysInfo sysInfo;
	
	/**
	 * 分页列表查询
	 *
	 * @param achExamPaperProjectItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-分页列表查询")
	@ApiOperation(value="ach_exam_paper_project_item-分页列表查询", notes="ach_exam_paper_project_item-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AchExamPaperProjectItem achExamPaperProjectItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req)  {
		Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
		//获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
		BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,achExamPaperProjectItem,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

		achExamPaperProjectItem.setCreateTime(null);
		achExamPaperProjectItem.setUpdateTime(null);
		QueryWrapper<AchExamPaperProjectItem> queryWrapper = QueryGenerator.initQueryWrapper(achExamPaperProjectItem, req.getParameterMap());
		Page<AchExamPaperProjectItem> page = new Page<AchExamPaperProjectItem>(pageNo, pageSize);
		IPage<AchExamPaperProjectItem> pageList = achExamPaperProjectItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	/**
	 * 全部Vo查询 附带关联名称
	 * listVoAll
	 */
	@AutoLog(value = "listVoAll-分页列表查询")
	@ApiOperation(value="listVoAll-分页列表查询", notes="listVoAll-分页列表查询")
	@GetMapping(value = "/listVoAll")
	public Result<?> listVoAll(AchExamPaperProjectItemVo achExamPaperProjectItemVo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//组装查询参数
		//组装vo以外的其他参数
		Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
		//获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值克隆给vo
		BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,achExamPaperProjectItemVo,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

		achExamPaperProjectItemVo.setCreateTime(null);
		achExamPaperProjectItemVo.setUpdateTime(null);
		Page<AchExamPaperProjectItemVo> page = new Page<AchExamPaperProjectItemVo>(pageNo, pageSize);
		//ptype = 1,表示排除总分分项获取其余
		IPage<AchExamPaperProjectItemVo> pageList = achExamPaperProjectItemService.listVoAll(page, achExamPaperProjectItemVo);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param achExamPaperProjectItem
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-添加")
	@ApiOperation(value="ach_exam_paper_project_item-添加", notes="ach_exam_paper_project_item-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AchExamPaperProjectItem achExamPaperProjectItem,HttpServletRequest req) {
		//获取当前登录用户信息
		String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
		String username = JwtUtil.getUsername(token);
		//		SysUser sysUser = sysUserService.getOneUser(username);// sysBaseAPI.getUserByName(username); 不能直接引用system模块，需使用APi调用
		LoginUser loginUser = sysBaseAPI.getUserByName(username); //.getUserById() 获取登录用户
		String tenantId = req.getHeader("tenant-id");//获取当前登录用户的租户id
		String sysOrgCode = loginUser.getOrgCode();//获取当前用户的当前的部门编码（不是id是编码）,该编码随前端切换

		achExamPaperProjectItem.setTenantId(tenantId);
		achExamPaperProjectItem.setSysOrgCode(sysOrgCode);
		achExamPaperProjectItemService.save(achExamPaperProjectItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param achExamPaperProjectItem
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-编辑")
	@ApiOperation(value="ach_exam_paper_project_item-编辑", notes="ach_exam_paper_project_item-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AchExamPaperProjectItem achExamPaperProjectItem) {
		achExamPaperProjectItemService.updateById(achExamPaperProjectItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-通过id删除")
	@ApiOperation(value="ach_exam_paper_project_item-通过id删除", notes="ach_exam_paper_project_item-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		achExamPaperProjectItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-批量删除")
	@ApiOperation(value="ach_exam_paper_project_item-批量删除", notes="ach_exam_paper_project_item-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.achExamPaperProjectItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ach_exam_paper_project_item-通过id查询")
	@ApiOperation(value="ach_exam_paper_project_item-通过id查询", notes="ach_exam_paper_project_item-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AchExamPaperProjectItem achExamPaperProjectItem = achExamPaperProjectItemService.getById(id);
		if(achExamPaperProjectItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(achExamPaperProjectItem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param achExamPaperProjectItem
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AchExamPaperProjectItem achExamPaperProjectItem) {
        return super.exportXls(request, achExamPaperProjectItem, AchExamPaperProjectItem.class, "ach_exam_paper_project_item");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AchExamPaperProjectItem.class);
    }

	 /**  根据试卷id 获取分项评分标准 Vo查询
	  * 附带评分标准
	  * getAchExamProjectItemStandard
	  * */
	 @AutoLog(value = "getAchExamProjectItemStandard-根据试卷id 获取分项评分标准")
	 @ApiOperation(value="getAchExamProjectItemStandard-根据试卷id 获取分项评分标准", notes="getAchExamProjectItemStandard-根据试卷id 获取分项评分标准")
	 @GetMapping(value = "/getAchExamProjectItemStandard")
	 public Result<?> getAchExamProjectItemStandard(String examinationPaperId,
													 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													 HttpServletRequest req) {
		 String tenantId = req.getHeader("tenant-id");
		 Page<AchExamPaperProjectItemVo> page = new Page<AchExamPaperProjectItemVo>(pageNo, pageSize);
		 IPage<AchExamPaperProjectItemVo> pageList = achExamPaperProjectItemService.getAchExamProjectItemStandard(page, tenantId, examinationPaperId);
		 return Result.OK(pageList);
	 }

	 /** 通过试卷ID 查询题型分项 */
	 @AutoLog(value = "paperProjectItemList-列表查询")
	 @ApiOperation(value="paperProjectItemList-列表查询", notes="paperProjectItemList-列表查询")
	 @GetMapping(value = "/paperProjectItemList")
	 public Result<?> paperProjectItemList(AchExamPaperProjectItem achExamPaperProjectItem,
								HttpServletRequest req) {
		 List<AchExamPaperProjectItem> itemList = achExamPaperProjectItemService.list(new QueryWrapper<AchExamPaperProjectItem>(achExamPaperProjectItem));
		 return Result.OK(itemList);
	 }



}
